// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.services' is found in services.js
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'highcharts-ng',
  'appItfAop.service',
  'starter.controllers', 'starter.services',
  'ionic.utils',//存储本地变量
  'ngCordova',//ncCordova
  'login.controller','login.service',//登录
  'maincontent.controller','maincontent.service',//主界面
  'actionlist.controller','actionlist.service',
  'prolist.controller','prolist.service',
  'newslist.controller','newslist.service',
  'newsview.controller','newsview.service',
  'mycenter.controller', 'mycenter.service',
  'aboutUs.controller', 'aboutUs.service',
  'notificationview.controller', 'notificationview.service',
  'tasklist.controller', 'tasklist.service',
  'custlist.controller', 'custlist.service',
  'feedback.controller', 'feedback.service',
  'notificationlist.controller', 'notificationlist.service',
  'stamain.controller', 'stamain.service',
])

// .run(function($ionicPlatform) {
  .run(['$ionicPlatform','$ionicPopup','$rootScope','$location','$ionicHistory','$ionicViewSwitcher',function($ionicPlatform,$ionicPopup,$rootScope,$location,$ionicHistory,$ionicViewSwitcher){
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);

    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
  });
  /*注册返回事件*/
  $ionicPlatform.registerBackButtonAction(function(e){
    /*用一个变量来控制界面上是否已经存在popup,多次点击的时候如果存在就不再提示*/
    $rootScope.popup = {
      isPopup: false,
      index: 0
    };
    function showConfirm(){
      var confirmPopup= $ionicPopup.confirm({
        title:"<strong>退出应用？</strong>",
        template:"你确定要退出吗？",
        okText:"我确定",
        cancelText:"取消"
      });
      $rootScope.popup.isPopup=true;
      confirmPopup.then(function(res){
        if(res){
          ionic.Platform.exitApp();
        }
      })
    }

    /*根据首页的路由判断是否提示退出*/
    if($location.path() =="/tab/maincontent"||$location.path() =="/login"){
      if(!$rootScope.popup.isPopup){
        showConfirm();
      }
    }else if($ionicHistory.backView()){
      $ionicHistory.goBack();//back到之前的路由
      $ionicViewSwitcher.nextDirection("back");//增加切换的样式
    }else{
      showConfirm();
    }
    return false;
  },100);//501表示优先级  100~999之间

}])

.config(function($stateProvider, $urlRouterProvider,$ionicConfigProvider) {

  $ionicConfigProvider.platform.ios.tabs.style('standard');
  $ionicConfigProvider.platform.ios.tabs.position('bottom');
  $ionicConfigProvider.platform.android.tabs.style('standard');
  $ionicConfigProvider.platform.android.tabs.position('standard');
  $ionicConfigProvider.platform.ios.navBar.alignTitle('center');
  $ionicConfigProvider.platform.android.navBar.alignTitle('left');
  $ionicConfigProvider.platform.ios.backButton.previousTitleText('').icon('ion-ios-arrow-thin-left');
  $ionicConfigProvider.platform.android.backButton.previousTitleText('').icon('ion-android-arrow-back');
  $ionicConfigProvider.platform.ios.views.transition('ios');
  $ionicConfigProvider.platform.android.views.transition('android');

  // Ionic uses AngularUI Router which uses the concept of states
  // Learn more here: https://github.com/angular-ui/ui-router
  // Set up the various states which the app can be in.
  // Each state's controller can be found in controllers.js
  $stateProvider
    .state('login',{ //登录
      url:'/login',
      templateUrl:'view/login.html',
      controller:'loginCtrl'
    })
    .state('tab', {
        url: '/tab',
        abstract: true,
        templateUrl: 'view/tabs.html'
      })
    .state('tab.maincontent', {
      url: '/maincontent',
      views: {
        'tab-maincontent': {
          templateUrl: 'view/maincontent.html',
          controller: 'mainContentCtrl'
        }
      }
    })
    .state('tab.actionlist', {
      url: '/actionlist',
      views: {
        'tab-actionlist': {
          templateUrl: 'view/actionlist.html',
          controller: 'actionlistCtrl'
        }
      }
    })
    .state('tab.prolist', {
      url: '/prolist',
      views: {
        'tab-prolist': {
          templateUrl: 'view/prolist.html',
          controller: 'prolistCtrl'
        }
      }
    })
    .state('tab.tasklist', {
        url: '/tasklist',
        views: {
            'tab-tasklist': {
                templateUrl: 'view/tasklist.html',
                controller: 'tasklistCtrl'
            }
        }
    })
    .state('tab.custlist', {
        url: '/custlist',
        views: {
            'tab-custlist': {
                templateUrl: 'view/custlist.html',
                controller: 'custlistCtrl'
            }
        }
    })
    .state('tab.mycenter', {
      url: '/mycenter',
      views: {
        'tab-mycenter': {
          templateUrl: 'view/mycenter.html',
          controller: 'mycenterCtrl'
        }
      }
    })
    .state('newslist',{
      url:'/newslist',
      templateUrl:'view/newslist.html',
      controller:'newslistCtrl'
    })
    .state('newsview',{
      url:'/newsview',
      templateUrl:'view/newsview.html',
      controller:'newsviewCtrl'
    })
    .state('notificationview', {
        url: '/notificationview',
        templateUrl: 'view/notificationview.html',
        controller: 'notificationviewCtrl'
    })
    .state('notificationlist', {
        url: '/notificationlist',
        templateUrl: 'view/notificationlist.html',
        controller: 'notificationlistCtrl'
    })
    .state('aboutUs', {
        url: '/aboutUs',
        templateUrl: 'view/aboutUs.html',
        controller: 'aboutUsCtrl'
    })
    .state('feedback', {
        url: '/feedback',
        templateUrl: 'view/feedback.html',
        controller: 'feedbackCtrl'
    })
    .state('stamain', {
        url: '/stamain',
        templateUrl: 'view/stamain.html',
        controller: 'stamainCtrl'
    })
    ;

  // if none of the above states are matched, use this as the fallback
  // $urlRouterProvider.otherwise('/maincontent');
  $urlRouterProvider.otherwise('/login');

})

//自定义过滤器 负数
.filter('customCurrency', ['$filter', function ($filter) {
  return function(amount){
    var rst = $filter('currency').apply(this, arguments);
    return amount >= 0 ? rst : rst.replace('(', '-').replace(')', '')
  };
}]);
